package com.example.demo.controller;


import com.example.demo.entity.Person;
import com.example.demo.entity.ResultData;
import com.example.demo.service.LoginService;
import com.example.demo.utils.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户控制类
 */
@Controller
@RequestMapping("/person")
public class LoginController {

    protected static final Logger logger = LoggerFactory.getLogger(com.example.demo.controller.LoginController.class);

    LoginService loginService;

    @Autowired
    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }


    @RequestMapping("/test")
    @RequiresRoles("user")
    public String test(){
        return "test";
    }

    @RequestMapping("/admin")
    @RequiresRoles("admin")
    public String amdin(){
        return "admin";
    }

    @RequestMapping("/user")
    public String user(){
        return "user";
    }



    @RequestMapping("/login")
    public String login(String user_name, String user_password){
        System.out.println(user_name+"  "+user_password);

        try {
            UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken(user_name,user_password);
            Subject subject= SecurityUtils.getSubject();
            subject.login(usernamePasswordToken);
        }catch (UnknownAccountException e){
            e.printStackTrace();
            System.out.println("用户名错误");
            return "login";
        } catch (IncorrectCredentialsException e){
            System.out.println("密码错误");
            e.printStackTrace();
            return "login";
        }catch (Exception e){
            e.printStackTrace();
            return "login";
        }
       return "success";
    }

    @RequestMapping("/findAll")
    @ResponseBody
    public ResultData<List<Person>> findAll(@RequestParam("user_lev") int user_lev){
        ResultData<List<Person>> data=new ResultData<>();
        System.out.println(user_lev);
        try {
            List<Person> result= new ArrayList<>();
            result=loginService.findall(user_lev);

            if(result!=null&&result.size()>=0){
                data.setCode(200);
                data.setMessage("查找成功");
                data.setData(result);
            }else {
                data.setCode(-1);
                data.setMessage("查找失败");
                data.setData(null);
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.info("对不起 登录出现问题");
        }
        return data;
    }

    @RequestMapping("/register")
    public String register(String user_name, String user_password,Integer user_lev){
        System.out.println(user_name+"  "+user_password);
        Person person=new Person();
        String salt=ShiroUtils.getRandomSalt(8);

        // 这里进行盐处理
        String password=ShiroUtils.md5(user_password,salt);
        person.setUser_name(user_name);
        person.setUser_password(password);
        person.setUser_address("");
        person.setUser_lev(user_lev);
        person.setUser_salt(salt);
        person.setUser_gender(1);
        person.setUser_money(0.0);
        System.out.println("存储的"+person);
        Boolean result=loginService.addPerson(person);
        if(result){
            return "login";
        }else {
            return "register";
        }
    }
}
